package com.ysx.oa.user.service.impl;

import com.ysx.oa.role.bean.UserRoleBean;
import com.ysx.oa.role.dao.UserRoleDao;
import com.ysx.oa.role.service.UserRoleService;
import com.ysx.oa.student.bean.StudentBean;
import com.ysx.oa.student.dao.StudentDao;
import com.ysx.oa.teacher.bean.TeacherBean;
import com.ysx.oa.teacher.dao.TeacherDao;
import com.ysx.oa.user.bean.UserBean;
import com.ysx.oa.user.dao.UserDao;
import com.ysx.oa.user.dto.UserDto;
import com.ysx.oa.user.service.UserService;
import com.ysx.oa.user.utils.UserUtils;
import com.ysx.oa.utils.GetDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: 周海燕
 * @Date: 2021/11/21 16:19
 */
@Service
public class UserServiceImpl implements UserService {

    // 目前多模块进行链接，暂时不需要Autowired注解
    @Autowired
    private UserDao userDao;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private TeacherDao teacherDao;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private UserRoleDao userRoleDao;

    private UserDto userDto = new UserDto();

    /**
     * 方法描述:注册
     *
     * @author: 周海燕
     * @date: 2021/11/21 16:32
     * @params: [userBean], [roleBean]=roleId
     * @returns:int
     */
    @Override
    public Integer register(UserBean userBean) {

        // md5加密    Spring自带的工具DigestUtils
        userBean.setPassword(UserUtils.md5(userBean));

        Integer userId = userDao.findUserIdByUserBean(userBean);

        // 确认表中是否有账号
        if (userId == null) {
            // 如果没有账号，判断新拿到的账号长度是否为11
            if (userBean.getPhone().length() == 11) {
                userBean.setCancelFlag(500100);

                userBean.setCreateTime(GetDateTime.nowTime());
                userBean.setLastUpdateTime(GetDateTime.nowTime());
                // 符合长度为11即可插入数据库
                return userDao.insert(userBean);
            }
        } else {
            return 0;
        }
        return 0;
    }


    /**
     * 方法描述:登录功能
     * 注1：根据用户输入账号密码查找用户id，并存储与session中
     * 注2：根据用户id查找角色id，并返回给前端
     *
     * @author: 周海燕
     * @date: 2021/11/22 1:32
     * @params: [userBean]=phone,password
     * @returns:java.lang.Integer
     */
    @Override
    public UserRoleBean login(UserBean userBean) {

        // 将拿到的密码加密
        userBean.setPassword(UserUtils.md5(userBean));

        // 将用户输入的账号密码存至userBean，查询表中是否存在该账号，并且账号密码完全匹配
        UserBean user = userDao.selectOne(userBean);

        // 新建userRoleBean用于查询中间表是否有该用户，有的话代表已分配角色
        UserRoleBean userRoleBean = new UserRoleBean();

        // 如果查询的结果不为空，并且账号状态正常
        if (user != null && user.getCancelFlag() == 500100) {

            // 拿到userId
            Integer userId = user.getId();

            // 根据用户id去kaoqin_user_role_rs表中查userId字段是否有该userid
            userRoleBean.setUserId(userId);
            UserRoleBean userAndRole = userRoleDao.selectOne(userRoleBean);


            // 如果selectUserId的size不等于0，代表有userId
            if (userAndRole != null) {

                // 拿到数组中的roleId
                Integer roleId = userAndRole.getRoleId();

                // 将userId和roleId都返回
                userRoleBean.setRoleId(roleId);
            } else {
                // 返回1=有用户但是未分配角色
                userRoleBean.setUserId(1);
            }
        } else {
            // 返回0=无用户或账号已注销
            userRoleBean.setUserId(0);
        }
        return userRoleBean;
    }


    /**
     * 方法描述:旧的注册：注册信息：手机号码，密码，姓名，性别，邮箱     自动生成userId
     *
     * @author: 周海燕
     * @date: 2021/11/23 15:06 [userBean] java.lang.Integer
     */
    /*@Override
    public Integer register(UserBean userBean) {
        UserRoleBean userRoleBean=new UserRoleBean();
        StudentBean studentBean=new StudentBean();
        TeacherBean teacherBean=new TeacherBean();


        // md5加密    Spring自带的工具DigestUtils
        userBean.setPassword(UserUtils.md5(userBean));

        String jobNum = getJobNum(userBean);
        userBean.setJobNum(jobNum);

        userBean.setCancelFlag(0);

        userBean.setCreateTime(GetDateTime.nowTime());
        userBean.setLastUpdateTime(GetDateTime.nowTime());

        int row = userDao.insert(userBean);
        System.out.println(row);

        // 查找该用户的userId
        UserBean user = userDao.selectOne(userBean);
        Integer userId = user.getId();
        Integer roleId = userBean.getRoleId();
        System.out.println(user);

        // 调用UserRoleService的add方法进行用户角色绑定（将用户信息插入表中）
        userRoleBean.setUserId(userId);
        userRoleBean.setRoleId(roleId);
        userRoleService.add(userRoleBean);
        System.out.println(userRoleBean);

        // 将用户加入对用模块表中
        if(roleId==10040){
            studentBean.setId(userId);
            studentDao.insert(studentBean);
        }else if(roleId==10050){
            teacherBean.setTeacherId(userId);
            teacherDao.insert(teacherBean);
        }
        return row;
    }*/
    @Override
    public Integer delete(UserBean userBean) {
        return userDao.delete(userBean);
    }

    @Override
    public List<UserBean> selectAll() {
        // 请求链接
        // url:"http://localhost:8090/kaoqin/user/find.do"
        return userDao.selectAll();
    }

    @Override
    public UserBean findUserByUserId(Integer userId) {
        return userDao.findUserByUserId(userId);
    }

    @Override
    public UserDto updateUserInfo(UserDto userDto) {


        // 完善用户信息：性别，邮箱，工号，最后修改时间
        UserBean userBean = new UserBean();

        userBean.setId(userDto.getId());
        userBean.setGender(userDto.getGender());
        userBean.setMail(userDto.getMail());
        userBean.setJobNum(UserUtils.getJobNum(userDto));
        userBean.setLastUpdateTime(GetDateTime.nowTime());
        userDao.updateByPrimaryKeySelective(userBean);

        Integer roleId = userDto.getRoleId();

        if (roleId == 20020) {

            // 调用学生的接口，完善：学历，就业状态
            StudentBean studentBean = new StudentBean();

            studentBean.setId(userDto.getId());
            studentBean.setEducation(userDto.getEducation());
            studentBean.setWhetherEmploy(userDto.getWhetherEmploy());
            studentBean.setUpdateDate(GetDateTime.nowTime());
            studentDao.updateByPrimaryKeySelective(studentBean);

        } else if (roleId == 20030) {
            // 调用老师的接口，完善：工资，入职时间
            TeacherBean teacherBean = new TeacherBean();

            teacherBean.setTeacherId(userDto.getTeacherId());
            teacherBean.setTeacherSalary(userDto.getTeacherSalary());
            teacherBean.setHireDate(userDto.getHireDate());
            teacherDao.updateByPrimaryKeySelective(teacherBean);
        }
        return userDto;
    }

    @Override
    public List<UserBean> findAllUserOrderByLastUpdateTime() {
        List<UserBean> userBean = userDao.findAllUserOrderByLastUpdateTime();
        return userBean;
    }


}
